<?php
namespace Home\Model;

class UserAddressModel extends \Common\Model\CommonModel{
    
    /* protected $_link = array(
        'user' =>array(
            'mapping_type'=>self::BELONGS_TO,
            'class_name'=>'Users',
            'foreign_key'=>'uid',
            'mapping_fields' =>'pic,nickname',
            'as_fields' =>'pic,nickname',
        ),
    ); */
    //根据用户ID获取收货地址信息
    public function get_consignee($user_id)
    {
        $fields = 'consignee,country,province,city,district,address,zipcode,tel,mobile,sign_building,best_time';
        $ads_data = $this->field($fields)->where(array('user_id'=>$user_id,'is_default'=>'1'))->find();
        if(empty($ads_data)){
            return $this->field($fields)->where(array('user_id'=>$user_id))->find();
        }else{
            return $ads_data;
        }
    }
    //检查是否有默认地址
    public function check_default($user_id){

        return  $this->where(array('user_id'=>$user_id,'is_default'=>'1'))->find();
    }
    //获取默认地址
    public function get_address($user_id){
        if($addr = $this->where(array('user_id'=>$user_id,'is_default'=>'1'))->find()){
            return $addr;
        }else{
            return $this->where(array('user_id'=>$user_id))->find();
        }
    }
    //地址列表
    public function address_list($user_id){
        if(empty($user_id)) return false;
        $list = $this->where(array('user_id'=>$user_id))->select();
        $region = D('Region');
        foreach ($list as $key=>$val){
            $province = $region->where(array('region_id'=>$val['province']))->getField('region_name');
            $city = $region->where(array('region_id'=>$val['city']))->getField('region_name');
            $district = $region->where(array('region_id'=>$val['district']))->getField('region_name');
            $list[$key]['address'] = $province.$city.$district.$val['address'];
        }
        return $list;
    }
}